import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
// 
const ProjectDetailModal	= $Lazy.state(()=>import('./cpt/project-detail-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms : [
			{ label: '条码号',		name: 'spec_code' },
			{ label: '报告单编号',	name: 'report_num'  },
			{ label: '合并规则',		name: 'merge_group_id',		type: 'select', loading:true },
		]
	}
	
	model = {  }
	
	componentDidMount(){
		this.getSelect()
		
		this.fetch()
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 合并规则
			$fn.setRow(forms, 'merge_group_id',  await f.getCache({ api:'merge_select' } ))
			//
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'merge-report/index')
	
	// 表单
	cols = [
		{ title: '条码号',			dataIndex: 'spec_code', 		...$fn.table.spec_code },
		{ title: '报告单编号', 		dataIndex: 'report_num', 		width:250 },
		{ title: '报告单模板', 		dataIndex: 'report_name', 		width:150 },
		{ title: '姓名',			dataIndex: 'patient_name', 		...$fn.table.name },
		{ title: '规则名称', 		dataIndex: 'merge_name', 		width:150 },
		{ title: '处理方法', 		dataIndex: 'inspec_unit', 		width:100 },
		{ title: '创建日期',			dataIndex: 'created_at', 		...$fn.table.time },
		TableControls.controls.call(this,{ width: 300 })
	]
	// 表格操作按钮
	table_controls = (text,{ report_num, uuid, spec_code }) => [
		Confirm.call(this, '召回报告单', 'merge-report/stop', { query:{ report_num } }),	
		TableControls.pushTo.call(this,'预览报告单', `preview-report?spec_num=${report_num}&spec_code=${spec_code}`),
		TableControls.click.call(this,'查看项目明细', () => this.projectDetailModalRef.open(uuid) )
	]
	// 操作按钮
	controls = () => [
		Controls.back.call(this),		// 返回
	]
	
	render(){
		return (
			<>
				<Page title='合并项目列表' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { type: -1 }) }
				</Page>
				<ProjectDetailModal onRef={ ref => this.projectDetailModalRef = ref } />
			</>
		)
	}
}

export default Index